//
//  SettingViewController.m
//  B
//
//  Created by lanou on 16/5/14.
//  Copyright © 2016年 裴启航. All rights reserved.
//

#import "SettingViewController.h"
#import "AboutViewController.h"
#import "QHClearCacheCell.h"
#import "SVProgressHUD.h"
#define kOut @"http://app.dunkhome.com/users/sign_out.json"  // 退出
#define kPut @"http://182.92.73.182:80/dunkhome/dunkhome/users/dunkhome"
@interface SettingViewController () <UITableViewDelegate, UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@end

@implementation SettingViewController
- (void)viewDidAppear:(BOOL)animated {
    self.navigationController.navigationBar.tintColor = [UIColor grayColor];
    //修改导航条标题颜色
    self.navigationItem.title = @"设置";
    [self.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor grayColor],NSForegroundColorAttributeName,nil]];
}
- (void)viewWillDisappear:(BOOL)animated {
    [self.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor],NSForegroundColorAttributeName,nil]];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"Cell"];
    [self.tableView registerClass:[QHClearCacheCell class] forCellReuseIdentifier:@"cacheCell"];
    self.tableView.scrollEnabled = NO; // 滑动关闭
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 2;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 20;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        return 100;
    }
    return 44;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 0) {
        return 1;
    } else {
        return 3;
    }
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 1 && indexPath.row == 0) {
        QHClearCacheCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cacheCell" forIndexPath:indexPath];
        [cell updateStatus];
        return cell;
    } else {
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
        if (indexPath.section == 0) {
            NSString *login = [[NSUserDefaults standardUserDefaults] objectForKey:@"result"];
            if ([login isEqualToString:@"登录"]) { // 表示用户账号已经登录
                cell.textLabel.text = [[NSUserDefaults standardUserDefaults] objectForKey:@"nickName"];
            } else {
                cell.textLabel.text = @"";
            }
        } else if (indexPath.section == 1 && indexPath.row == 1) {
            cell.textLabel.text = @"关于";
        } else {
            cell.textLabel.text = @"退出账号";
        }
        return cell;
    }
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    // 取消选中
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (indexPath.section == 0) {
    } else {
        if (indexPath.row == 0) {
            QHClearCacheCell *cell = (QHClearCacheCell *)[tableView cellForRowAtIndexPath:indexPath];
            [cell clearCache];
        } else if (indexPath.row == 1) {
            AboutViewController *about = [AboutViewController new];
            [self.navigationController pushViewController:about animated:YES];
            return;
        } else {
            UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleAlert];
            alert.message = @"确定退出账号?";
            [alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
                [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"result"];
                [[NSUserDefaults standardUserDefaults] setObject:@"注销" forKey:@"result"];
                [[NSUserDefaults standardUserDefaults] synchronize];
                [self.tableView reloadData];
            }]];
            [alert addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
            }]];
            [self presentViewController:alert animated:YES completion:nil];
        }
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
